﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EyFramework.AutoCode.Entity;
using EyFramework.Common;
using SqlSugar;

namespace EyFramework.AutoCode.DbFunc
{
    public partial class SqlServerPanel : UserControl, IDbPanel
    {
        private SqlserverDb _sqlserverDb;
        private bool _addFlag = false;

        public SqlServerPanel()
        {
            _addFlag = true;
            InitializeComponent();
        }

        public SqlServerPanel(SqlserverDb sqlserverDb)
        {
            InitializeComponent();
            this._sqlserverDb = sqlserverDb;
            teConnectNm.Text = sqlserverDb.ConnectName;
            teHostName.Text = sqlserverDb.HostIp;
            teDatabaseName.Text = sqlserverDb.DataBaseName;
            teUserName.Text = sqlserverDb.UserName;
            tePassword.Text = sqlserverDb.Password;
        }

        public bool SaveDbConnect()
        {
            if (!CheckData())
            {
                return false;
            }
            FillData();
            if (_addFlag)
            {
                PublicGlobal.DataBases.SqlserverDbs.Add(_sqlserverDb);
            }
            return true;
        }

        private void FillData()
        {
            if (_addFlag)
            {
                _sqlserverDb = new SqlserverDb();
            }
            _sqlserverDb.ConnectName = teConnectNm.Text.Trim();
            _sqlserverDb.HostIp = teHostName.Text.Trim();
            _sqlserverDb.DataBaseName = teDatabaseName.Text.Trim();
            _sqlserverDb.UserName = teUserName.Text.Trim();
            _sqlserverDb.Password = tePassword.Text.Trim();
        }

        private bool CheckData()
        {
            if (string.IsNullOrEmpty(teConnectNm.Text.Trim()))
            {
                Globals.ShowWarning("连接名称不能为空");
                teConnectNm.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(teHostName.Text.Trim()))
            {
                Globals.ShowWarning("主机IP不能为空");
                teHostName.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(teDatabaseName.Text.Trim()))
            {
                Globals.ShowWarning("数据库名不能为空");
                teDatabaseName.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(teUserName.Text.Trim()))
            {
                Globals.ShowWarning("用户名不能为空");
                teUserName.Focus();
                return false;
            }
            return true;
        }

        /// <summary>
        /// 测试连接
        /// </summary>
        /// <returns></returns>
        public bool TestConnect()
        {
            if (!CheckData())
            {
                return false;
            }

            #region 创建数据库连接
            string ConnectStr = "Data Source={0};Database={1};User ID={2};Password={3}";
            ConnectStr = string.Format(ConnectStr, teHostName.Text.Trim(),teDatabaseName.Text.Trim(),teUserName.Text.Trim(),tePassword.Text.Trim());
            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = ConnectStr,//必填, 数据库连接字符串
                DbType = SqlSugar.DbType.SqlServer,         //必填, 数据库类型
                IsAutoCloseConnection = true       //默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
            }))
            {
                try
                {
                    db.Ado.CheckConnection();
                    return true;
                }
                catch (Exception ex)
                {
                    Globals.ShowError("数据库连接失败," + ex.ToString());
                    return false;
                }
            }
            #endregion
        }
    }
}
